{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import tkinter\n",
    "import tkinter.simpledialog\n",
    "import tkinter.colorchooser\n",
    "import tkinter.filedialog\n",
    "#需要执行pip install pillow安装扩展库\n",
    "from PIL import ImageGrab\n",
    "\n",
    "root = tiker.TK()\n",
    "root.title('My Paint----by Dong fuguo')\n",
    "root.['width'] = 800\n",
    "rooy.['height'] = 600\n",
    "\n",
    "#控制是否允许画图的变量， 1：允许，0：不允许\n",
    "canDraw = tkinter.IntVar(value=0)\n",
    "\n",
    "#控制画图类型的变量，1：曲线，2：直线，3：矩形，4：文本，5：橡皮\n",
    "what = tkinter.IntVar(value=1)\n",
    "\n",
    "#记录鼠标位置的变量\n",
    "X = tkinter.IntVar(value=0)\n",
    "Y = tkinter.IntVar(value=0)\n",
    "\n",
    "#前景色，背景色\n",
    "foreColor = '#OOOOOO'\n",
    "backColor = '#FFFFFF'\n",
    "\n",
    "#创建画布\n",
    "image = tkinter.PhotoImage()\n",
    "canvas = tkinter.Canvas(root,\n",
    "                bg='white,'\n",
    "                width=800,height=600,)\n",
    "canvas.create_image(800,600,image=image)\n",
    "\n",
    "#鼠标左键单击，允许画图，并记录当前鼠标位置\n",
    "def onLeftButtonDown(event):\n",
    "    canDraw.set(1)\n",
    "    X.set(event.x)\n",
    "    Y.set(event.y)\n",
    "    if what.get()==4:\n",
    "        canvas.create_text(event.x,event.y,text=text)\n",
    "canvas.bind('<Button-1>',onLeftButtonDown)\n",
    "\n",
    "#记录最后绘制图形的id\n",
    "lastDraw = 0\n",
    "#按住鼠标左键移动，画图\n",
    "def onLeftButtonMove(event):\n",
    "    global lastDraw\n",
    "    if canDraw.get()==0:\n",
    "        return\n",
    "    if what.get()==1:\n",
    "        #使用当前选择的前景色绘制曲线\n",
    "        canvas.creat_line(X.get(),\n",
    "                    Y.get(),\n",
    "                    event.x,\n",
    "                    event.y,\n",
    "                    fill=foreColor)\n",
    "        X.set(event.x)\n",
    "        Y.set(event.y)\n",
    "    elif what.get()==2:\n",
    "        #绘制直线，先删除刚刚画过的直线，再画一条新的直线\n",
    "        try:\n",
    "            canvas.delete(lastDraw)\n",
    "        except Exception as e:\n",
    "            pass\n",
    "        lastDraw = canvas.create_line(X.get(),\n",
    "                            Y.get(),\n",
    "                            event.x,\n",
    "                            event.y,\n",
    "                            fill=foreColor)\n",
    "    elif what.get()==3:\n",
    "        #绘制矩形，先删除刚刚画过的矩形，再画一个新的矩形\n",
    "        try:\n",
    "            canvas.delete(lastDraw)\n",
    "        except Exception as e:\n",
    "            pass\n",
    "        lastDraw = canvas.create_rectangle(X.get(),\n",
    "                               Y.get(),\n",
    "                               event.x,\n",
    "                               event.y,\n",
    "                               fill=backColor,\n",
    "                               outline=foreColor)\n",
    "    elif what.get()==5:\n",
    "        #橡皮，使用背景色填充10*10的矩形区域\n",
    "        canvas.create_rectangle(event.x-5,\n",
    "                                event.y-5,\n",
    "                                event.x+5,\n",
    "                                event.y+5,\n",
    "                                outline=backColor,\n",
    "                                fill=backColor)\n",
    "canvas.bind('<B1-Motion>',onLeftButtonMove)\n",
    "\n",
    "#鼠标左键抬起，不允许画图了\n",
    "def onLeftButtonUp(event):\n",
    "    if what.get()==2:\n",
    "        #绘制直线\n",
    "        canvas.create_line(X.get(),\n",
    "                    Y.get(),\n",
    "                    event.x,\n",
    "                    event.y,\n",
    "                    fill=foreColor)\n",
    "    elif what.get()==3:\n",
    "        #绘制矩形\n",
    "        canvas.create_rectangle(X.get(),\n",
    "                                Y.get(),\n",
    "                                event.x,\n",
    "                                event.y,\n",
    "                                fill=backColor,\n",
    "                                outline=foreColor)\n",
    "    canDraw.set(0)\n",
    "    global lastDraw\n",
    "    #防止切换图形时误删上次绘制的图形\n",
    "    lastDraw = 0\n",
    "canvas.bind('<ButtonRelease-1>',onLeftButtonUp)\n",
    "\n",
    "#创建菜单\n",
    "menu = tkinter.Menu(root,tearoff=0)\n",
    "#打开图像文件\n",
    "def Open():\n",
    "  filename = tkinter.filedialog.askopenfilename(title ='Open Image',\n",
    "                filetypes = [('image','*.jpg*.png*.gif')])\n",
    "  if filename:\n",
    "    global image\n",
    "    image = tkinter.PhotoImage(file=filename)\n",
    "    canvas.create_image(80,80,image=image)\n",
    "menu.add_command(label='Open',command=Open)\n",
    "\n",
    "def Save():\n",
    "  #获取客户区域位置和尺寸，并截图保存\n",
    "  left = int(root.winfo_rootx())\n",
    "  top = int(root.winfo_rooty())\n",
    "  width = int(root.winfo_width())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
